package com.atguigu.flink.chapter05.Transform;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/8/9 10:30
 */
public class Flink04_Filter {
    public static void main(String[] args) throws Exception {
        // 0.执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 1.读取数据
        //  sensor_1,123123213,10
        DataStreamSource<String> socketDS = env.socketTextStream("localhost", 9999);

        socketDS
                .map((MapFunction<String, WaterSensor>) value -> {
                    String[] datas = value.split(",");
                    return new WaterSensor(
                            datas[0],
                            Long.parseLong(datas[1]),
                            Integer.parseInt(datas[2])
                    );
                })
                .filter(sensor -> sensor.getVc() > 10 )
                .print();

        env.execute();
    }
}
